﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using tmAsk.Common;

namespace tmAsk.User
{
    public class User_data
    {
        tmAsk.Data.MakehtmlSQL SQL = new tmAsk.Data.MakehtmlSQL();

        public static string Session_str = "UserLogin";
        public User_data()
        {
        }

        /// <summary>
        /// 注册会员
        /// </summary>
        /// <param name="model">用户模型</param>
        /// <returns>返回添加 ID</returns>
        public int Add_user(User_model model)
        {
            //自动注册会员
            int uid = 0;
            string sql = "insert into B_user(u_email,u_passWord,u_nickName,u_askNumber,u_reviewNumber,u_goodNumber,u_regTime,u_point,u_level,u_ischeck,u_img,u_email) values(";
            sql += "@u_email,@u_passWord,@u_nickName,@u_askNumber,@u_reviewNumber,@u_goodNumber,@u_regTime,@u_point,@u_level,@u_ischeck,@u_img,@u_email)";
            SqlParameter[] eparameters = new SqlParameter[]{
                new SqlParameter("@u_email", model.u_email),
                new SqlParameter("@u_passWord", model.u_passWord),
                new SqlParameter("@u_nickName", model.u_nickName),
                new SqlParameter("@u_askNumber", model.u_askNumber),
                new SqlParameter("@u_reviewNumber", model.u_reviewNumber),
                new SqlParameter("@u_goodNumber", model.u_goodNumber),
                new SqlParameter("@u_regTime", model.u_regTime),
                new SqlParameter("@u_point", model.u_point),
                new SqlParameter("@u_level", model.u_level),
                new SqlParameter("@u_ischeck", model.u_ischeck),
                new SqlParameter("@u_img", model.u_img),
                new SqlParameter("@u_email", model.u_email)
                 };
            uid = SQL.Add(sql, eparameters);
            return uid;

        }

        /// <summary>
        /// 修改会员信息
        /// </summary>
        /// <param name="model">用户模型</param>
        /// <returns>返回影响行数</returns>
        /// 
        public int Update_user(User_model model)
        {
            //自动注册会员
            int uid = 0;
            string sql = "update B_user set ";
            if (model.u_passWord != null)
            {
                sql += "u_passWord='" + model.u_passWord + "',";
            }
            if (model.u_nickName != null)
            {
                sql += "u_nickName='" + model.u_nickName + "',";
            }
            if (model.u_askNumber != 0)
            {
                sql += "u_askNumber=" + model.u_askNumber + ",";
            }
            if (model.u_reviewNumber != 0)
            {
                sql += "u_reviewNumber=" + model.u_reviewNumber + ",";
            }
            if (model.u_goodNumber != 0)
            {
                sql += "u_goodNumber=" + model.u_goodNumber + ",";
            }
            if (model.u_regTime != null)
            {
                sql += "u_regTime='" + model.u_regTime + "',";
            }
            if (model.u_point != 0)
            {
                sql += "u_point=" + model.u_point + ",";
            }
            if (model.u_level != 0)
            {
                sql += "u_level=" + model.u_level + ",";
            }
            if (model.u_ischeck != 0)
            {
                sql += "u_ischeck=" + model.u_ischeck + ",";
            }
            if (model.u_img != null)
            {
                sql += "u_img='" + model.u_img + "',";
            }
            if (model.u_ischeck != 0)
            {
                sql += "u_email='" + model.u_email + "'";
            }
            sql += " where u_id = " + model.u_id;
            uid = SQL.ExecuteSql(sql);// 修改用户信息
            return uid;

        }

        /// <summary>
        /// 删除会员信息
        /// </summary>
        /// <param name="id">User Id</param>
        /// <returns>影响行数</returns>
        /// 
        public int Delete_user(int id)
        {
            return SQL.ExecuteSql("delete from B_user where u_id=" + id);
        }

        ////---------------------用户相关操作
        public int Reg_user(string password, string Config_defaultname, string email)
        {
            function make_htm = new function();
            User_model model = new User_model();

            string tmpRootDir = HttpContext.Current.Request.ApplicationPath.ToString();

            model.u_email = email;
            model.u_passWord = make_htm.Md5(password);
            model.u_nickName = Config_defaultname;
            model.u_askNumber = 0;
            model.u_reviewNumber = 0;
            model.u_goodNumber = 0;
            model.u_img = "/images/face/default.jpg";
            model.u_ischeck = 0;
            model.u_level = 0;
            model.u_point = Convert.ToInt16( Common.alldata.Config_point);
            model.u_regTime = DateTime.Now.ToString();
            return Add_user(model);
        }

        /// <summary>
        /// 根据用户名，返回信息表
        /// </summary>
        /// <param name="filenames">用户登录名</param>
        /// <returns></returns>
        public DataTable Get_user(string username)
        {
            // return SQL.GetList("select "+filenames+" from B_user "+ where).Tables[0];
            string sql = "select u_id,u_email,u_password,u_ischeck from b_user where u_email = @username";
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@username", SqlDbType.Char),
            };
            parameters[0].Value = username;
            DataSet ds = SQL.GetList(sql, parameters);
            return ds.Tables[0];
        }
    }
}
